//
//  ShopM.m
//  Pizza
//
//  Created by sendInfo on 2015-04-13.
//  Copyright (c) 2015年 sendInfo. All rights reserved.
//

#import "ShopM.h"

@interface ShopM()
{

     int _page;
}

@end

@implementation ShopM
#pragma mark -

- (void)firstPage
{
    
    _page = 1;
    [self gotoPage:1];
    
}
- (void)nextPage
{
    
    [self gotoPage:self.shopsArray.count/10+1];
    _page = self.shopsArray.count/10+1;
    
}
- (void)gotoPage:(NSUInteger)page
{
    self.CANCEL_MSG(HttpAPI.shops);
    

    self.MSG(HttpAPI.shops).INPUT(@"body",@{@"source":@"IOS",@"action":self.shopType,@"pageNumber":[NSString stringWithFormat:@"%d",_page],@"pageSize":@"10"});
    

    
}

ON_MESSAGE3(HttpAPI, shops, msg)
{
    
    if (msg.sending) {
        
        
        [self sendUISignal:self.RELOADING];
        
    }else {
        
        
        if (msg.succeed) {
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                
            }
            
            if (_page == 1) {
                
                [self.shopsArray removeAllObjects];
                self.shopsArray = msg.GET_OUTPUT(@"shops");
                
            }else{
                
                [self.shopsArray addObjectsFromArray:msg.GET_OUTPUT(@"shops")];
                int all = [msg.GET_OUTPUT(@"totallNum") intValue];
                self.more = self.shopsArray.count >= all? NO : YES;
                //[self saveCache];
                
            }
            

            
            
        }else if (msg.failed)
        {
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
        
        [self sendUISignal:self.RELOADED];
        
        
    }
}

@end
